package dubbo.server4.impl.common;

import java.lang.reflect.Field;
import java.util.Date;

public class ObjectToObjectUtil {
	/** 
	 * 
	 * @param obj       ת��Դ���� 
	 * @param toObj     ת������ 
	 * @param <T>       ת����� 
	 * @return 
	 * @throws Exception 
	 */  
	public static <T> T objSimpleConvert(Object obj,Class<T> toObj)throws Exception{  
	    T toObjIns = (T)toObj.newInstance(); //����Ŀ�����ʵ��  
	    Class sourCls = obj.getClass();  
	    //����Դ����  
	    do{  
	        Field[] sourFlds = sourCls.getDeclaredFields(); //Դ���Լ�  
	        for(int i = 0 ; i < sourFlds.length; i++){ //����Դ��������  
	            Field sf = sourFlds[i];  
	            sf.setAccessible(true);  
	              System.out.println(sf.getName());  
	            //����Ŀ����������  
	            Class toCls = toObj;  
	            do{  
	                Field[] toFlds = toCls.getDeclaredFields(); //Դ���Լ�  
	                for(int j = 0 ; j < toFlds.length; j++){ //����Դ��������  
	                    Field tof = toFlds[j];  
	                    tof.setAccessible(true);  
	                    if(sf.getName().equals(tof.getName())){ //����������ͬ  
	                        String type = tof.getType().toString();//�õ������Ե�����  
	                        if (type.endsWith("String")) {  
	                            tof.set(toObjIns,(String)sf.get(obj));  
	                        }else if(type.endsWith("int") || type.endsWith("Integer")){  
	                            tof.set(toObjIns,(Integer)sf.get(obj));  
	                        }else if(type.endsWith("Date")){  
	                            tof.set(toObjIns,(Date)sf.get(obj));  
	                        }else if(type.endsWith("long") || type.endsWith("Long")){  
	                            tof.set(toObjIns,(Long)sf.get(obj));  
	                        }else if(type.endsWith("short") || type.endsWith("Short")){  
	                            tof.set(toObjIns,(Short)sf.get(obj));  
	                        }else if(type.endsWith("boolean")|| type.endsWith("Boolean")){  
	                            tof.set(toObjIns,(Boolean)sf.get(obj));  
	                        }
	                        else {  
	                            //throw new Exception("����ת��ʧ�ܣ�");  
	                        }  
	                    }  
	                }  
	                toCls = toCls.getSuperclass();  
	  
	            }while(toCls != Object.class);  
	        }  
	        sourCls = sourCls.getSuperclass();  
	    }while(sourCls != Object.class);  
	    return toObjIns;  
	}  
}
